﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using TS_XY_Train_Repaire.UserCon.Stuff;

namespace TS_XY_Train_Repaire.UserCon.Tools.Stuff
{
    public partial class uc_Department_Select_By_One : DevExpress.XtraEditors.XtraForm
    {
        public string _Depid = "-1";
        public uc_Department_Select_By_One()
        {
            InitializeComponent();
        }

        private void uc_Department_Select_By_One_Load(object sender, EventArgs e)
        {
            LeverString_SysOrganization _ls = new LeverString_SysOrganization();
            tl_Main.Nodes.Clear();
            tl_Main.Refresh();
            ArrayList pList = new ArrayList();
            string strSQL = "select * from TS_S_T_P_Department where f_rsid="+TS_Win_Locomotive_MainForm._SysSettings._GetRailwayStations+" and f_del=0 and f_deep=0 order by f_id asc";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            SqlDataReader read = cmd.ExecuteReader();
            string _strDepQuery = " and (";
            int i = 0;
            while (read.Read())
            {
                if (i == 0)
                {
                    _strDepQuery += "(f_level like ',%" + read["f_id"].ToString() + "%,')";
                }
                else
                {
                    _strDepQuery += " or (f_level like ',%" + read["f_id"].ToString() + "%,')";
                }
                _ls = new LeverString_SysOrganization();
                _ls.f_id = read["f_id"].ToString();
                _ls.f_name = read["f_name"].ToString();
                _ls.f_code = read["f_code"].ToString();
                _ls.f_deep = read["f_deep"].ToString();
                _ls.f_level = read["f_level"].ToString();
                _ls.f_rbid = read["f_rbid"].ToString();
                _ls.f_rsid = read["f_rsid"].ToString();
                _ls.f_imgindex = 3;
                pList.Add(_ls);
                i++;
            }
            _strDepQuery += ")";
            read.Close();
            strSQL = "select * from TS_S_T_P_Department where f_rsid=" + TS_Win_Locomotive_MainForm._SysSettings._GetRailwayStations + " " + _strDepQuery + " and f_deep>0 and f_del=0 order by f_id asc";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                _ls = new LeverString_SysOrganization();
                _ls.f_id = read["f_id"].ToString();
                _ls.f_name = read["f_name"].ToString();
                _ls.f_code = read["f_code"].ToString();
                _ls.f_deep = read["f_deep"].ToString();
                _ls.f_level = read["f_level"].ToString();
                _ls.f_rbid = read["f_rbid"].ToString();
                _ls.f_rsid = read["f_rsid"].ToString();
                string _UPID = "";
                _UPID = read["f_level"].ToString();
                _UPID = _UPID.Substring(0, _UPID.Length - 1);
                _UPID = _UPID.Substring(_UPID.LastIndexOf(',') + 1);
                _ls.f_parent = _UPID;
                _ls.f_imgindex = 3;
                pList.Add(_ls);
            }
            read.Close();
            conn.Close();
            conn.Dispose();

            tl_Main.DataSource = pList;
            tl_Main.ExpandAll();
            try
            {
                tl_Main.SetFocusedNode(tl_Main.FindNodeByFieldValue("f_id", _Depid));
            }
            catch { }
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {
            uc_Stuff_Main_Info_ManaForm._rDepName = tl_Main.FocusedNode["f_name"].ToString();
            uc_Stuff_Main_Info_ManaForm._rDepID = tl_Main.FocusedNode["f_id"].ToString();
            this.DialogResult = DialogResult.OK;
        }
    }
}